// pages/add/add.js
let app = getApp();
Page({
  data: {
    showModal: false,
    termArray: [],
    termIndex: 0,
    weekList: [
      { id: 1, checked: false },
      { id: 2, checked: false },
      { id: 3, checked: false },
      { id: 4, checked: false },
      { id: 5, checked: false },
      { id: 6, checked: false },
      { id: 7, checked: false },
      { id: 8, checked: false },
      { id: 9, checked: false },
      { id: 10, checked: false },
      { id: 11, checked: false },
      { id: 12, checked: false },
      { id: 13, checked: false },
      { id: 14, checked: false },
      { id: 15, checked: false },
      { id: 16, checked: false },
      { id: 17, checked: false },
      { id: 18, checked: false },
      { id: 19, checked: false },
      { id: 20, checked: false }
    ],
    weeks:[],
    termId: '',
    kctitle: '',
    place: '',
    content: '',
    week: '',
    xingqi: '',
    jieci: '',
    changdu: '',
    remindtime: '',
    flag: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    let that = this;
    that.getTermList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  getTermList: function() {
    let that = this;
    wx.request({
      url: app.globalData.url + '/findTerm',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userInfo.id
      },
      success: res => {
        console.log("获取用户的学期列表")
        console.log(res)
        if (res.data.length > 0) {
          let list = [];
          for (var i = 0; i < res.data.length; i++) {
            list.push({
              id: res.data[i].id,
              name: res.data[i].name
            })
          }
          that.setData({
            termArray: list
          })
        }
      }
    });
  },

  bindTermChange: function(e) {
    let that = this;
    let selectedterm = that.data.termArray[e.detail.value];
    console.log(selectedterm)
    that.setData({
      termId: that.data.termArray[e.detail.value].id,
      termIndex: e.detail.value
    })
  },
  selectWeek: function() {
    this.setData({
      showModal: true
    })
  },
  weekChange: function(e) {
    let that = this;
    let weekList = that.data.weekList; 
    // 若之前已选择，将其回显打勾 
    for (let i = 0; i < weekList.length; i++) {
      weekList[i].checked = false;
    }
    let indexes = e.detail.value;
    for (let i = 0; i < indexes.length; i++) {
      indexes[i] = parseInt(indexes[i]); 
      // 多选框从1开始，数组下标从0开始，所以需要减1 
      weekList[indexes[i] - 1].checked = true;
    } 
    // 直接将整个list赋值回去 
    this.setData({
      weekList: weekList
    })

  },
  // 用户点击确定 
  onConfirm: function () {
    let that = this; 
    let weekList = that.data.weekList;
    let weeks = that.data.weeks; 
    weekList.forEach(function (e) {
      if (e.checked) {
        // gzkind是对象的一个属性，表示具体公章类型名的字符串 
        weeks.push(e.id);
      }
    });
    // 显示到wxml上 
    that.setData({ 
      week: weeks.join(","),
      showModal: false 
    }) 
  },
  onCancel: function(){
    this.setData({
      showModal: false
    })
  },
  
  inputTitleChange: function(e) {
    this.setData({
      kctitle: e.detail.value
    })
  },
  inputPlaceChange: function(e) {
    this.setData({
      place: e.detail.value
    })
  },
  inputContentChange: function(e) {
    this.setData({
      content: e.detail.value
    })
  },
  inputXingqiChange: function(e) {
    this.setData({
      xingqi: e.detail.value
    })
  },
  inputJieciChange: function(e) {
    this.setData({
      jieci: e.detail.value
    })
  },
  inputChangduChange: function(e) {
    this.setData({
      changdu: e.detail.value
    })
  },
  //  点击提醒时间组件确定事件
  bindTimeChange: function(e) {
    this.setData({
      remindtime: e.detail.value
    })
  },
  flagChange: function(e) {
    let that = this;
    console.log(e)
    that.setData({
      flag: e.detail.value.length
    })
  },
  addSchedule: function() {
    var that = this;
    //添加课程信息
    wx.request({
      url: app.globalData.url + '/createSchedule',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        termId: that.data.termId,
        kctitle: that.data.kctitle,
        place: that.data.place,
        content: that.data.content,
        weekList: that.data.weeks,
        xingqi: that.data.xingqi,
        jieci: that.data.jieci,
        changdu: that.data.changdu,
        remindtime: that.data.remindtime,
        flag: that.data.flag
      },
      success: res => {
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 3000
        });
        that.onLoad();
      },
      fail: err => {
        wx.showToast({
          title: '失败',
          icon: 'error',
          duration: 3000
        });
      }
    });
  }
})